import { queryFakeList,
  getCoopActInfoList,
  pageGetActPrd} from '../services/api';
import errMassage from '../utils/errMassage';

export default {
  namespace: 'list',

  state: {
    list: [],
    companyInfo:{},
    prdData:{},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'queryList',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *appendFetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'appendList',
        payload: Array.isArray(response) ? response : [],
      });
    },

    *getCoopActInfoList({ payload }, { call, put }) {
      const response = yield call(getCoopActInfoList, payload);
      if(response && response.success === 1){
        yield put({
          type: 'queryList',
          payload: response.data.list,
        });
        yield put({
          type: 'queryCompanyInfo',
          payload: response.data.companyInfo,
        });
      }else{
        errMassage(response.msg);
      }
    },
    *pageGetActPrd({ payload }, { call, put }) {
      const response = yield call(pageGetActPrd, payload);
      if(response && response.success === 1){
        yield put({
          type: 'queryActPrdList',
          payload: {
            actPrdLis : response.data.list,
            pagination: {
              total: response.data.totalPage * response.data.pageSize,
              pageSize: response.data.pageSize,
              current: response.data.currentPage * 1 + 1,
            },
        },
        });
      }else{
        errMassage(response.msg);
      }
    },
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    queryActPrdList(state, action) {
      return {
        ...state,
        prdData: action.payload,
      };
    },
    queryCompanyInfo(state, action) {
      return {
        ...state,
        companyInfo: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
  },
};
